package com.example.industry.controller;

import com.example.industry.entity.Project;
import com.example.industry.entity.Task;
import com.example.industry.entity.User;
import com.example.industry.enums.Roles;
import com.example.industry.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/user")
public class UserController
{
    @Resource
    private UserService userService;

    /**
     * 通过Id查找用户
     * @param userId 用户Id
     * @return 用户
     */
    @GetMapping(value = "/{userId}")
    public User findUserById(@PathVariable("userId") Integer userId)
    {
        return userService.findUserById(userId);
    }

    /**
     * 注册
     * @param username 用户名
     * @param email 邮箱
     * @param password 密码
     * @return 是否成功
     */
    @PostMapping(value = "/register")
    public boolean register(@RequestParam(value = "username") String username,
                            @RequestParam(value = "email") String email,
                            @RequestParam(value = "password") String password)
    {
        return userService.register(username, email, password);
    }

    /**
     * 登陆
     * @param email 邮箱
     * @param password 密码
     * @param session 会话
     * @return 是否成功
     */
    @PostMapping(value = "/login")
    public boolean login(@RequestParam(value = "email") String email,
                         @RequestParam(value = "password") String password, HttpSession session)
    {
        return userService.login(email, password, session);
    }

    /**
     * 注销
     * @param session 会话
     */
    @PostMapping(value = "/logout")
    public void logout(HttpSession session)
    {
        userService.logout(session);
    }

    /**
     * 获取用户参与的项目, 角色为拥有者OWNER
     * @return 项目集合
     */
    @GetMapping(value = "/projects/owner")
    public List<Project> findOwnerProjectsByUserId(HttpSession session)
    {
        User user = (User)session.getAttribute("user");
        if (user == null) return null;
        return userService.findProjectsByUserIdAndRole(user.getUserId(), Roles.OWNER);
    }

    /**
     * 获取用户参与的项目, 角色为管理员ADMIN
     * @return 项目集合
     */
    @GetMapping(value = "/projects/admin")
    public List<Project> findAdminProjectsByUserId(HttpSession session)
    {
        User user = (User)session.getAttribute("user");
        if (user == null) return null;
        return userService.findProjectsByUserIdAndRole(user.getUserId(), Roles.ADMIN);
    }

    /**
     * 获取用户参与的项目, 角色为普通COMMON
     * @return 项目集合
     */
    @GetMapping(value = "/projects/common")
    public List<Project> findCommonProjectsByUserId(HttpSession session)
    {
        User user = (User)session.getAttribute("user");
        if (user == null) return null;
        return userService.findProjectsByUserIdAndRole(user.getUserId(), Roles.COMMON);
    }

    /**
     * 查询分配的任务
     * @return 任务集合
     */
    @GetMapping(value = "/task/assign")
    public List<Task> findAssignTasksByUserId(HttpSession session)
    {
        User user = (User)session.getAttribute("user");
        if (user == null) return null;
        return userService.findAssignTasksByUserId(user.getUserId());
    }

    /**
     * 查询被指派的任务
     * @return 任务集合
     */
    @GetMapping(value = "/task/accept")
    public List<Task> findAcceptTaskSByUserId(HttpSession session)
    {
        User user = (User)session.getAttribute("user");
        if (user == null) return null;
        return userService.findAcceptTaskSByUserId(user.getUserId());
    }

}
